package edu.service;


import java.util.List;

import edu.dao.UserDAO;
import edu.dao.UserRoleDAO;
import edu.model.User;


public class UserService {
	
	private int userId;
	private UserDAO userDAO = null;
	private UserRoleDAO userRoleDAO = null;

	public int getUserId() {
		return userId;
	}
	public void setUserId(int userId) {
		this.userId = userId;
	}
	public UserDAO getUserDAO() {
		return userDAO;
	}
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
		
	public void setUserRoleDAO(UserRoleDAO userRoleDAO) {
		this.userRoleDAO = userRoleDAO;
	}
	public UserRoleDAO getUserRoleDAO() {
		return userRoleDAO;
	}
	public User getUserById(int userId){
		return userDAO.findById(userId);
	}
	public void saveUser(User user){
		userDAO.save(user);
	}
	
	public boolean deleteUser(User use){
		
		userDAO.delete(use);
		return true;
	}
	
	public boolean updateUser(User user){
		User u = userDAO.findById(user.getUserId());
		if(u != null){
			userDAO.attachDirty(user);
			return true;
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	public List<User> getAllUserOderBy(String name, String order){
		return userDAO.findAllOderBy(name, order);
	}
	
	@SuppressWarnings("unchecked")
	public User getUser(String username, String password){

		List<User>userList = userDAO.findByUserName(username);
		if(!userList.isEmpty()){
			for(User user : userList){
				if(password.equals(user.getUserPassword())){
					if(!user.getUserRoles().isEmpty()) {
						return user;
					} 
				}
			}
		}
		return null;
	}


}